package com.ctcc.serviceImpl;

import com.ctcc.entity.Employee;
import com.ctcc.entity.Employee_temp;
import com.ctcc.repository.EmpeRepository;
import com.ctcc.repository.EmpeRepositoryTemp;
import com.ctcc.util.HttpUtil;
import com.mashape.unirest.http.JsonNode;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * Created by Peanut on 2018/2/1.
 */
@Service
public class EmployeeImpl {
    Log log = LogFactory.getLog(EmployeeImpl.class);
    @Autowired
    HttpUtil httpUtil;
    @Autowired    EmpeRepository empeRepository;
    @Autowired    EmpeRepositoryTemp empeRepositoryTemp;

    public int getAllEmpl(String url, JsonNode jsonNode) throws Exception {
        int count = 0;
        if (StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(jsonNode.toString())) {

            String jsonStr = httpUtil.org_yunzhijia(url, jsonNode).toString();
            if (StringUtils.isNotEmpty(jsonStr)) {
                log.info("获取接口数据成功");
            }
            JSONObject jsonObject = new JSONObject(jsonStr);
            log.info(jsonObject.get("success"));
            log.info(jsonObject.get("errorCode"));
            if ("100".equals(jsonObject.get("errorCode").toString())) {
                JSONArray jsonEmployeeArray = jsonObject.getJSONArray("data");
                if (jsonEmployeeArray.length() > 0) {
                    count = jsonEmployeeArray.length();

                    for (int i = 0; i < jsonEmployeeArray.length(); i++) {
                        log.info("开始新导入数据，第" + i + "    ");
                        Employee_temp employee_temp = new Employee_temp();
                        JSONObject jsonObjectEmployee = (JSONObject) jsonEmployeeArray.get(i);
                        employee_temp.setDepartment(jsonObjectEmployee.getString("department"));
                        employee_temp.setBirthday(jsonObjectEmployee.getString("birthday"));
                        employee_temp.setName(jsonObjectEmployee.getString("name"));
                        employee_temp.setContact(jsonObjectEmployee.getString("contact").toString());
                        employee_temp.setWeights(jsonObjectEmployee.getString("weights"));
                        employee_temp.setOrgId(jsonObjectEmployee.getString("orgId"));
                        employee_temp.setUid(jsonObjectEmployee.getString("uid"));
                        employee_temp.setOpenId(jsonObjectEmployee.getString("openId"));
                        employee_temp.setPhotoUrl(jsonObjectEmployee.getString("photoUrl"));
                        employee_temp.setPhone(jsonObjectEmployee.getString("phone"));
                        employee_temp.setIsHidePhone(jsonObjectEmployee.getString("isHidePhone"));
                        employee_temp.setEmail(jsonObjectEmployee.getString("email"));
                        employee_temp.setJobTitle(jsonObjectEmployee.getString("jobTitle"));
                        employee_temp.setJobNo(jsonObjectEmployee.getString("jobNo"));
                        employee_temp.setGender(jsonObjectEmployee.getInt("gender"));
                        employee_temp.setStatus(jsonObjectEmployee.getInt("status"));
                        employee_temp.setOrgUserType(jsonObjectEmployee.getInt("orgUserType"));
                        empeRepositoryTemp.save(employee_temp);
                        log.info("导入第" + i + "数据成功");
                    }


                }

            } else {
                log.info("获取数据失败，无法导入数据");
            }

        }
        return count;
    }


    /***
     * 查出全部数据
     */
    public List<Employee> getAllEmpl() {
        return empeRepository.findAll();
    }

    /***
     * 查出临时表全部数据
     */
    public List<Employee_temp> getAllEmplTemp() {
        return empeRepositoryTemp.findAll();
    }

    /**
     * 清空临时表
     */
    public void removeEmplTemp() {
        empeRepositoryTemp.deleteAll();
    }

    public void saveAlltoEmpl() {

        List<Employee> collectionAllEmpl = getAllEmpl();
        List<Employee_temp> collectionAllEmplTemp = getAllEmplTemp();

        List<Employee> tempEmp = collectionAllEmplTemp.stream().flatMap(temp -> {
            Employee employee = new Employee();
            employee.setDepartment(temp.getDepartment());
            employee.setBirthday(temp.getBirthday());
            employee.setName(temp.getName());
            employee.setContact(temp.getContact().toString());
            employee.setWeights(temp.getWeights());
            employee.setOrgId(temp.getOrgId());
            employee.setUid(temp.getUid());
            employee.setOpenId(temp.getOpenId());
            employee.setPhotoUrl(temp.getPhotoUrl());
            employee.setPhone(temp.getPhone());
            employee.setIsHidePhone(temp.getIsHidePhone());
            employee.setEmail(temp.getEmail());
            employee.setJobTitle(temp.getJobTitle());
            employee.setJobNo(temp.getJobNo());
            employee.setGender(temp.getGender());
            employee.setStatus(temp.getStatus());
            employee.setOrgUserType(temp.getOrgUserType());
            return Stream.of(employee);
        }).collect(Collectors.toList());

        Collection<Employee> collectionCompared = CollectionUtils.subtract(tempEmp,collectionAllEmpl );
        Collection<Employee> allEmp = collectionCompared;
        for (Employee emp : allEmp) {
            saveOneEmpl(emp);
        }
    }

    public void saveOneEmpl(Employee employee_temp) {
        Employee employee = new Employee();
        if (employee_temp != null) ;
        employee.setDepartment(employee_temp.getDepartment());
        employee.setBirthday(employee_temp.getBirthday());
        employee.setName(employee_temp.getName());
        employee.setContact(employee_temp.getContact().toString());
        employee.setWeights(employee_temp.getWeights());
        employee.setOrgId(employee_temp.getOrgId());
        employee.setUid(employee_temp.getUid());
        employee.setOpenId(employee_temp.getOpenId());
        employee.setPhotoUrl(employee_temp.getPhotoUrl());
        employee.setPhone(employee_temp.getPhone());
        employee.setIsHidePhone(employee_temp.getIsHidePhone());
        employee.setEmail(employee_temp.getEmail());
        employee.setJobTitle(employee_temp.getJobTitle());
        employee.setJobNo(employee_temp.getJobNo());
        employee.setGender(employee_temp.getGender());
        employee.setStatus(employee_temp.getStatus());
        employee.setOrgUserType(employee_temp.getOrgUserType());
        empeRepository.save(employee);

    }

}
